package richbar.com.richbar.fragment.product;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseFragment;
import com.common.help.RecycleViewHelp;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.product.ProductDetailsAc;
import richbar.com.richbar.base.BaseListAdapter;
import richbar.com.richbar.bean.CommentResult;
import richbar.com.richbar.bean.GoodsInfoResult;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.view.NoScrollGridView;

/**
 * Created by QiYang on 2018/11/27.
 */

public class ProductCommentFragment extends BaseFragment {
    private SwipeRefreshLayout swipe;
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    private ProductDetailsAc ac;
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_comment, container, false);
        swipe = view.findViewById(R.id.swipe);
        recyclerView = view.findViewById(R.id.recycleView);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        ac = (ProductDetailsAc) context;
    }

    @Override
    protected void initData() {
        swipe.setRefreshing(true);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(false);
            }
        });
        adapter = new MyAdapter(R.layout.item_product_comment,new ArrayList<CommentResult.DataEntity.RowsEntity>());
        RecycleViewHelp.setManager(recyclerView,getActivity());
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getData(true);
            }
        });

        getData(false);
    }

    private void getData(final boolean loadMore) {
        if (loadMore){
            pageAdd();
        }else {
            pageReset();
        }
        NetWorkUtil.getRateList(currentPage(), "6", ac.getGoodsId(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                refreshDismiss(swipe);
                final CommentResult result = MyGson.getInstance().fromJson(data, CommentResult.class);
                if (checkIsOk(result)){
                    setData(result,loadMore);
                }
            }
        });
    }

    private void setData(CommentResult result, boolean loadMore) {
        adapter.setNewData(result.getData().getRows());
    }

    class MyAdapter extends BaseQuickAdapter<CommentResult.DataEntity.RowsEntity> {
        public MyAdapter(int layoutResId, List<CommentResult.DataEntity.RowsEntity> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, CommentResult.DataEntity.RowsEntity bean) {
            holder.setText(R.id.comment_nickname1,bean.getNickname());
            ImageView head = holder.getView(R.id.comment_head1);
            GlideUtil.loadImage(getActivity(),NetWorkUtil.URL + bean.getHead(),head);
            holder.setText(R.id.comment1,bean.getContent());
            holder.setText(R.id.type1,bean.getAttr());
            NoScrollGridView gridView = holder.getView(R.id.gridView);
            GridAdapter adapter = new GridAdapter(getActivity(),bean.getImg());
            gridView.setAdapter(adapter);
        }
    }

    class GridAdapter extends BaseListAdapter<CommentResult.DataEntity.RowsEntity.ImgEntity> {
        public GridAdapter(Context context, List<CommentResult.DataEntity.RowsEntity.ImgEntity> data) {
            super(context, data);
        }

        @Override
        public View convert(int i, View view, ViewGroup viewGroup, CommentResult.DataEntity.RowsEntity.ImgEntity bean) {
            View v = LayoutInflater.from(getActivity()).inflate(R.layout.item_pic,null);
            ImageView pic = v.findViewById(R.id.item_pic);
            GlideUtil.loadImage(context,NetWorkUtil.URL + bean.getImgpath(),pic);
            return v;
        }
    }

}
